if( SERVER )then AddCSLuaFile( "shared.lua" ); end

if( CLIENT ) then
	
	SWEP.PrintName			= "Custom 11";
	SWEP.Author				= "Nev";
	SWEP.Slot				= 3;
	SWEP.SlotPos			= 3;
	SWEP.Purpose			= "Fires a grenade";
	SWEP.Instructions		= "Left Click: Fire, Right Click: Detonate grenades";
	
	SWEP.Category			= "SMOD";
	
end

SWEP.Spawnable				= true;
SWEP.AdminSpawnable			= false;

SWEP.ViewModel				= "models/weapons/v_rpg.mdl";
SWEP.WorldModel				= "models/weapons/w_rocket_launcher.mdl";

SWEP.Weight					= 25;
SWEP.AutoSwitchTo			= false;
SWEP.AutoSwitchFrom			= false;

SWEP.Primary =
{
	
	ClipSize				= 50,
	DefaultClip				= 50,
	Automatic				= false,
	Ammo					= "RPG_Round"
	
};

SWEP.Secondary =
{
	
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= false,
	Ammo					= "none"
	
};

SWEP.Sounds =
{
	
	Fire = Sound( "weapons/rpg/rocketfire1.wav" ),
	Beep = Sound( "buttons/blip1.wav" ),
	Detonate = Sound( "weapons/shotgun/shotgun_cock.wav" )
	
};

function SWEP:Initialize( )
	
	if( SERVER )then self:SetWeaponHoldType( "rpg" ); end
	
end

function SWEP:PrimaryAttack( )
	
	if( self.Weapon:CanPrimaryAttack( ) ) then
		
		if( SERVER ) then
			
			local pos = self.Owner:GetShootPos( );
			local ang = self.Owner:GetAimVector( );
			
			local proj = ents.Create( "prop_physics" );
			
			if( ValidEntity( proj ) )then
				
				proj:SetModel( "models/weapons/w_grenade.mdl" );
				proj:SetPos( pos );
				proj:SetAngles( ang:Angle( ) );
				proj:SetOwner( self.Owner );
				
				proj:Spawn( );
				proj:Activate( );
				
				local phys = proj:GetPhysicsObject( );
				
				if( ValidEntity( phys ) ) then
					
					phys:Wake( );
					phys:ApplyForceCenter( ang * phys:GetMass( ) * 750 );
					
				end
				
				self.Owner.GrenadeList = self.Owner.GrenadeList or { };
				table.insert( self.Owner.GrenadeList, proj );
				
			end
			
		end
		
		self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK );
		self.Owner:SetAnimation( PLAYER_ATTACK1 );
		
		self.Owner:EmitSound( self.Sounds.Fire );
		
		self.Weapon:TakePrimaryAmmo( 1 );
		
	end
	
	self.Weapon:SetNextPrimaryFire( CurTime( ) + 0.09 );
	
	return false;
	
end

function SWEP:Reload( )
	
	self.Weapon:DefaultReload( ACT_VM_RELOAD );
	
	return false;
	
end

function SWEP:SecondaryAttack( go )
	
	local grenades = self.Owner.GrenadeList;
	if( !grenades or ( grenades and #grenades <= 0 ) ) then return; end
	
	if( !go ) then
		
		self.Weapon:EmitSound( self.Sounds.Detonate );
		
		timer.Simple( 0.5, self.SecondaryAttack, self, true );
		return;
		
	end
	
	local c = 0;
	
	for i, proj in pairs( grenades ) do
	
		// effect data
		local fx = EffectData( );
		fx:SetOrigin( proj:GetPos( ) );
		fx:SetMagnitude( 1.8 );
		fx:SetScale( 3.5 );
		fx:SetRadius( 5.5 );

		//spark effect
		util.Effect( "sparks", fx, true, true );
				
		local function callback( )
		
			if( ValidEntity( proj ) && ValidEntity( self.Owner ) && ValidEntity( self.Weapon ) ) then
			
				proj:EmitSound( self.Sounds.Beep );
			
				local pos = proj:GetPos( );
				
				//effect data
				local fx = EffectData( );
				fx:SetStart( pos );
				fx:SetOrigin( pos );
				fx:SetScale( 1 )

				//effect
				util.Effect( "Explosion", fx );

				//damage
				util.BlastDamage( self.Weapon, self.Owner, pos, 230, 90 );
				
				proj:Remove( );
			
			end
		
		end
		timer.Simple( i * .07, callback );
		
		grenades[ i ] = nil;
		
		c = c + i * .07;
		
	end
	
	self.Weapon:SetNextSecondaryFire( CurTime( ) + c + 0.09 );
	
	return false;
	
end

function SWEP:Deploy( )
	
	self.Weapon:SendWeaponAnim( ACT_VM_DRAW );
	
	return true;
	
end